/**
 * Created with IntelliJ IDEA.
 * User: skyline{http://my.oschina.net/skyline520}
 * Date: 12-11-22
 * Time: 下午9:30
 * To change this template use File | Settings | File Templates.
 */
Ext.define('Airways.controller.system.roleuser.RoleUserManageController',{
    extend:'Ext.app.Controller',
    models:['system.user.UserManageModel','system.role.RoleManageModel'],
    views:['system.roleuser.RoleUserManageView'],
    refs:[
        {ref:'roleUserManageView',selector:'roleUserManageView'},
        {ref:'roleUserRoleGridPanel',selector:'roleUserRoleGridPanel'},
        {ref:'roleLeftView',selector:'roleLeftView'},
        {ref:'roleRightView',selector:'roleRightView'},
        {ref:'roleUserRoleQueryFormPanel',selector:'roleUserRoleQueryFormPanel'},
        {ref:'roleUserUserGridPanel',selector:'roleUserUserGridPanel'},
        {ref:'roleUserUserQueryFormPanel',selector:'roleUserUserQueryFormPanel'}
    ],
    init:function (){
        var controller = this;
        //记录勾选的记录id
        var idArray = new Ext.util.MixedCollection();
        controller.idArray = idArray;
        this.control({
             'roleUserManageView':{
                 afterrender:function(){
                 }
             },
            'roleUserRoleQueryFormPanel button[action=query]':{
                click:function(){
                    controller.getRoleUserRoleGridPanel().getStore().loadPage(1);
                }
            },
            'roleUserUserQueryFormPanel button[action=query]':{
                click:function(){
                    var re = controller.record;
                    if(re == null){
                        Ext.MessageBox.show({title:'提示',msg:'请先选择角色数据!',icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                        return;
                    }
                    controller.getRoleUserUserGridPanel().getStore().loadPage(1);
                }
            },
            'roleUserRoleGridPanel':{
                render:function(grid,opts){
                    var store=grid.getStore();
                    var _queryFormPanel = controller.getRoleUserRoleQueryFormPanel();
                    var button = _queryFormPanel.down("button[action=query]")
                    store.on({
                        load: function(){
//                            button.setDisabled(false);
                            button.enable();
                        },
                        beforeload:function(){
                            controller.record = null;
//                            button.setDisabled(true);
                            button.disable();
                            store.proxy.extraParams = _queryFormPanel.getForm().getFieldValues();
                        }
                    })
                },
                selectionchange: function(view, records) {
                    if (records[0]) {
                        controller.record = records[0];
                        controller.getRoleUserUserGridPanel().getStore().loadPage(1);
                    }
                }
            },
            'roleUserUserGridPanel':{
                render:function(grid,opts){
                    var store=grid.getStore();
                    var model = grid.selModel;
                    var _queryFormPanel = controller.getRoleUserUserQueryFormPanel();
                    var button = _queryFormPanel.down("button[action=query]")
                    model.on({
                        select : function (me, record,index,eOpts) {
                            if (!controller.idArray.contains(record.get('userId'))) {
                                controller.idArray.add(record.get('userId'));
                            }
                        },
                        deselect:function (me, record,index,eOpts) {
                            console.log("测试");
                            if(controller.idArray.indexOf(record.get('userId'))!=-1){
                                controller.idArray.remove(record.get('userId'));
                            }
                        }
                    });
                    store.on({
                        load: function(){
//                            button.setDisabled(false);
                            button.enable();
                            var arry = [];
                            store.each(function(record) {
                                if (record.data.checked == true){
                                    arry.push(record);
                                }
                                model.select(arry, true);
                            });
                        },
                        beforeload:function(){
                            model.removeListener('deselect');
                            var param = _queryFormPanel.getForm().getFieldValues();
                            if(controller.record == null){
                                Ext.MessageBox.show({title:'提示',msg:'请先选择角色数据!',icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                                return;
                            }
                            param.roleId = controller.record.data.roleId;
//                            button.setDisabled(true);
                            button.disable();
                            store.proxy.extraParams = param;
                        }
                    })
                }
            },
            'roleRightView button[action=save]':{
                click:function(){
                    var re = controller.record;
                    if(re == null){
                        Ext.MessageBox.show({title:'提示',msg:'请先选择角色数据!',icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                        return;
                    }
                    var _idArray = controller.idArray;
                    var url = basePath+'system/roleuser/saveRoleUser';
                    var params = {};
                    params.roleId = re.data.roleId;
                    params.version = re.data.version;
                    params.ids = _idArray.getRange().join(',');
                    //调用ajax 发送数据到后台
                    callAjax(url,params,this.handleResult);
                }
            },
            handleResult:function(){
                var idArray = new Ext.util.MixedCollection();
                controller.idArray = idArray;
            }
        });
    }
});
